import { Controller } from 'egg';

export default class TagController extends Controller {
  /**
   * 获取标签列表
   */
  async getTagList() {
    const { ctx } = this;
    const { type } = ctx.query;

    try {
      const whereCondition: any = {};
      if (type) {
        whereCondition.type = type;
      }

      // 从数据库读取标签数据
      const tags = await ctx.model.Tag.findAll({
        where: whereCondition,
        order: [['id', 'ASC']],
      });
      
      ctx.body = {
        code: 200,
        message: '获取成功',
        data: tags,
      };
    } catch (error: any) {
      ctx.throw(400, error.message);
    }
  }
}